<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--<%@ include file="../include/admincontent.jsp" %>--%>
<link rel="stylesheet" href="../resources/js/plugins/layui/css/layui.css"/>
<link rel="stylesheet" href="../resources/css/knowlegPoints.css"/>
<link rel="stylesheet" href="../resources/css/skin.css"/>
<link rel="stylesheet" href="../resources/css/index.css"/>

<div class="test-paper">
    <div id="location">当前位置：首页 &gt;笔试管理  &gt; 试卷管理</div>
    <div id="content">
        <div class="btnbar">
            <button id="test_add">
                <em class="ico16 add_new"></em>
                <span class="menu_span" title="添加试卷">添加试卷</span>
            </button>
            <button id="test_edit">
                <em class="ico16 editor_16"></em>
                <span class="menu_span" title="修改试卷">修改试卷</span>
            </button>
            <button id="test_del">
                <em class="ico16 enabled_16"></em>
                <span class="menu_span" title="删除试卷">删除试卷</span>
            </button>
            <button id="test_close">
                <em class="ico16 enabled_16"></em>
                <span class="menu_span" title="启用/停用">启用/停用</span>
            </button>
            <button id="test_view">
                <em class="ico16 preview_16"></em>
                <span class="menu_span" title="查看试卷">查看试卷</span>
            </button>
            <button id="test_manage">
                <em class="ico16 list_16"></em>
                <span class="menu_span" title="试卷题目管理">试卷题目管理</span>
            </button>
            <button id="test_answer">
                <em class="ico16 list_16_2"></em>
                <span class="menu_span" title="试卷答案">试卷答案</span>
            </button>
            <button id="test_export">
                <em class="ico16 behavioranalysis_down"></em>
                <span class="menu_span" title="导出试卷">导出试卷</span>
            </button>
        </div>
        <!-- 筛选条件 -->
        <div id="searchbox">
            <form id="searchfrom" action="">
                <table width="100%" class="treeTable searchform" cellpadding="0" cellspacing="0">
                    <thead>
                    <tr>
                        <td width="70">试卷用途：</td>
                        <td width="140">
                            <select name="sPurpose" id="sPurpose">
                                <option value="-1">所有</option>
                                <option value="0">练习用卷</option>
                                <option value="1">考试用卷</option>
                            </select>
                        <td width="45">岗位：</td>
                        <td width="140">
                            <select name="sPosition" id="sPosition">
                                <option value="">所有岗位</option>
                                <c:forEach var="baseJobs" items="${jobs}" varStatus="item" >
                                    <c:choose>
                                        <c:when test="${baseJobs!=null}">
                                            <option value="${baseJobs.name}">${baseJobs.name}</option>
                                        </c:when>
                                    </c:choose>
                                </c:forEach>
                            </select>
                        </td>
                        <td width="60">关键字：</td>
                        <td width="140"><input type="text" name="sKey" class="search-input" placeholder="请输入关键字"></td>
                        <td>
                            <button id="searchbtn" class="search" type="button">
                                <em class="ico16 search_16"></em>
                                <span class="menu_span" title="查询">查询</span>
                            </button>
                        </td>
                    </tr>
                    </thead>
                </table>
            </form>
        </div>
    </div>
    <table class="layui-hide" id="tablelist" lay-filter="paperTable"></table>
</div>

<div id="list-page" class="other-page">
    <div class="btnbar">
        <button class="save">
            <em class="ico16 save_traft_16"></em>
            <span class="menu_span" title="保存">保存</span>
        </button>
        <button class="back" onclick="javascript:location='/Knowledges/';">
            <em class="ico16 cancle_publish_16"></em>
            <span class="menu_span" title="返回">返回</span>
        </button>
        <span class="test-name">试卷名称：测试固定试卷</span>
    </div>
    <p></p>
    <table id="list-table" lay-filter="list-table"></table>
</div>

<div id="answer-page" class="other-page">
    <div class="btnbar">
        <button class="save">
            <em class="ico16 outport_16"></em>
            <span class="menu_span" title="导出答案">导出答案</span>
        </button>
        <span class="test-name">试卷名称：测试固定试卷</span>
    </div>
    <p></p>
    <table id="answer-table" lay-filter="answer-table"></table>
</div>

<script type="text/javascript" src="../resources/js/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="../resources/js/plugins/layui/layui.js"></script>
<script type="text/javascript" src="../resources/js/actions.js"></script>
<script src="../resources/js/fadeInout.js" type="text/javascript"></script>

<script type="text/javascript" language="javascript">
    var paperManageTable;
 //   $(function () {
        layui.use('table', function () {
            paperManageTable = layui.table

            paperManageTable.render({
                id:'paperTable',
                elem: '#tablelist',
                url: '/PaperManager/getPaperList?' + $("#searchform").serialize(), //数据接口
                page: true,
                cellMinWidth: 40, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                cols: [[
                        {type:'checkbox',fixed:'left', width: 40},
                        { field: 'paperName', title: '试卷名称', },
                        { field: 'paperPurpose', title: '试卷用途', width: 100,templet: function(d){
                                if(d.paperPurpose === "0")
                                    return '<span>练习用卷</span>';
                                else if(d.paperPurpose === "1")
                                    return '<span>考试用卷</span>';
                            }},
                        { field: 'paperType', title: '试卷类型', width: 100 ,templet: function(d){
                                if(d.paperPurpose === "0")
                                    return '<span>固定试卷</span>';
                                else if(d.paperPurpose === "1")
                                    return '<span>随机试卷</span>';
                            }},
                        { field: 'position', title: '所属岗位', width: 200 },
                        { field: 'totalScore', title: '总分', width: 100 },
                        { field: 'addName', title: '创建人', width: 100 },
                        { field: 'addTime', title: '创建时间', width: 200 },
                        { field: 'paperState', title: '状态', width: 80 },
                    ],
                ],
            })

            $("#searchbtn").on('click', () => {
                var  sPurposes = $("#sPurpose").val();
                var  sPositions = $("#sPosition").val();
                var  sKeys = $("input[name='sKey']").val();

                paperManageTable.reload('paperTable', {
                    url:'/PaperManager/getPaperList'
                    ,method: 'get'
                    ,page:  {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        key: {
                            sPurpose : sPurposes,
                            sPosition : sPositions,
                            sKey : sKeys,
                        }
                    }
                }, 'data');
            });


        })

    /**
     * 添加试卷
     */
    $("#test_add").on('click', (e) => {
        location = '/PaperManager/add';
    });
    /**
     * 查看试卷
     * */
    $("#test_view").on('click', (e) => {
        gotoPaper('/PaperManager/seePaper?id=',2,'查看试卷','60%', '60%');
      });
    /**
     **试卷答案
     */
    $('#test_answer').on('click', function() {
        gotoPaper('/PaperManager/answer?id=',2,'试卷答案','60%', '60%');
    });
        /**
     * 修改试卷
     */
    $("#test_edit").on('click', (e) => {
        gotoPaper('/PaperManager/add?id=',1,null,null,null);
    });

    function gotoPaper(urls,type,title,widths,heights) {
        layui.use('table', function () {
            var table = layui.table;
            var id = '';
            var checkStatusTemp = table.checkStatus('paperTable');

            if (checkStatusTemp.data.length < 1) {
                layer.alert("请选择一条信息！", {icon: 7});
            } else if (checkStatusTemp.data.length > 1) {
                layer.alert("只能选择一条信息！", {icon: 7});
            } else {
                id = checkStatusTemp.data[0].id_cb.replace('cb_', '') + '&';

                if(type === 1)
                    location = urls + id;
                else(type === 2)
                    openLayer(title, urls + id , widths, heights);
            }
        });
    }
    /**
     * 启用/停用试卷
     * */
        /**
         * 删除试卷
         * */
     $("#test_del").on('click', (e) => {
        layui.use(['table','layer'], function () {
            var table = layui.table;
            var id = '';
            var checkStatusTemp = table.checkStatus('paperTable'); //idTest 即为基础参数 id 对应的值

            if (checkStatusTemp.data.length === 0) {//未选中需要删除的数据
                layer.alert("请选择要执行该操作的信息！");
            } else {//删除选中的信息
                var delFlag = "false"; //是否包含非停用试卷
                var numm = 0;
                for (var i = 0; i < checkStatusTemp.data.length; i++) {
                    if(checkStatusTemp.data[i].paperState === "停用"){
                        id += 'id=' + checkStatusTemp.data[i].id_cb.replace('cb_', '') + '&';
                        numm++;
                    }
                    else
                        delFlag = "true";
                }
                if(numm === 0){
                    layer.msg("请选择“停用”的试卷！", {icon: 2});
                }else {
                    id += 'delFlag='+delFlag;

                    postAjax('/PaperManager/delPaper', id, function (data) {
                        if (data.state) {
                            layer.msg(data.Message, {icon: 1})

                            //删除成功，重载数据
                            paperManageTable.reload('paperTable', {
                                url: '/PaperManager/getPaperList?' + $("#searchform").serialize()
                                , where: {} //设定异步数据接口的额外参数
                            });
                        } else {
                            layer.msg(data.Message, {icon: 2})
                        }
                    });
                }

            }
        });
     });


    /**
     * 导出试卷
     */
    $("#test_export").on('click', (e) => {
        layui.use('table', function () {
            var table = layui.table;
            var id = '';
            var checkStatusTemp = table.checkStatus('paperTable'); //idTest 即为基础参数 id 对应的值

            if (checkStatusTemp.data.length === 0) {//未选中需要删除的数据
                layer.alert("请选择要执行该操作的信息！");
            }else if (checkStatusTemp.data.length > 10) {
                parent.layer.alert("一次最多只能选择10条信息！", {icon: 2});
            }  else {//删除选中的信息
                for (var i = 0; i < checkStatusTemp.data.length; i++) {
                    id += 'id=' + checkStatusTemp.data[i].id_cb.replace('cb_', '') + '&';
                }

                postAjax('/PaperManager/exportPaper', id, function (json) {
                    if(json.state){
                        var urls = json.url.split(',');
                        var lens = urls.length-1;
                        for(var i=0;i<lens;i++){
                            if (window.navigator.appName === 'Microsoft Internet Explorer') {  // IE
                                window.open(urls[i], '_blank');
                            } else {
                                let a = document.createElement('a') ;// 创建a标签
                                let e = document.createEvent('MouseEvents'); // 创建鼠标事件对象
                                e.initEvent('click', false, false); // 初始化事件对象
                                a.href = urls[i] ;// 设置下载地址
                                a.download = ''; // 设置下载文件名
                                a.dispatchEvent(e);
                            }
                        }
                        layer.alert(json.Message+"\n已下载"+lens+"份试卷。", {icon: 1});
                    }else{
                        layer.alert(json.Message, {icon: 2});
                    }
                });
            }
        });
    });

    /**
     * 试卷题目管理
     */
    $("#test_manage").on('click', (e) => {
        gotoPaper('/PaperManager/topic?id=',1,null,null,null);
    });
//})
</script>
